package hotel.switchs.oversea.biz.service.impl;


import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.JacksonUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.ResultStatusInfoVo;
import hotel.switchs.oversea.api.VO.SwitchCountryInfoVo;
import hotel.switchs.oversea.api.entity.SwitchCountryInfo;
import hotel.switchs.oversea.biz.mapper.SwitchCountryInfoMapper;
import hotel.switchs.oversea.biz.service.SwitchCountryInfoService;
import hotel.switchs.oversea.biz.utils.SwitchsHttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * @author zhangjinzheng
 * @date 2019-11-22
 */
@Service
public class SwitchCountryInfoServiceImpl extends BaseServiceImpl<SwitchCountryInfoVo> implements SwitchCountryInfoService {
    Logger logger= LoggerFactory.getLogger(SwitchCountryInfoServiceImpl.class);

    @Value("${profile.type}")
    private String profile;

    @Autowired
    private SwitchCountryInfoMapper switchCountryInfoMapper;


    /**
     * 获取国家列表
     * getdltcountrylist
     * @return
     */
    @Override
    public Result<Object> getdltcountrylist() {
        Map<String, Object> data = new HashMap<String, Object>();
        String result= SwitchsHttpUtils.sendRequest(data,"getdltcountrylist",profile);
        JSONObject jsonObject = JSONObject.parseObject(result);
        System.out.println(result);
        ResultStatusInfoVo resultStatusInfoVo= JacksonUtil.parse(jsonObject.getString("resultStatus"),ResultStatusInfoVo.class);
        //说明调用接口失败
        if(resultStatusInfoVo.getResultCode()!=0){
            logger.info("错误信息{}",resultStatusInfoVo.getResultMsg());
            return null;
        }
        JSONObject countryInfoObject = jsonObject.getJSONObject("countryInfoList");
        if (ObjectUtils.isEmpty(countryInfoObject)) {
            return null;
        }
        System.out.println(result);
        List<SwitchCountryInfoVo> switchCountryInfolists = JacksonUtil.jsonToList(countryInfoObject.getString("countryInfo"), SwitchCountryInfoVo.class);
        //获取的国家列表数据入库
        List<SwitchCountryInfo> switchCountryInfoEntities=new ArrayList<>();
        for(SwitchCountryInfoVo switchCountryInfo:switchCountryInfolists){
            SwitchCountryInfo switchCountryInfoEntity=new SwitchCountryInfo();
            switchCountryInfoEntity.setCountryId(Integer.valueOf(switchCountryInfo.getCountryID()));
            switchCountryInfoEntity.setCountryName(switchCountryInfo.getCountryName());
            switchCountryInfoEntity.setId(String.valueOf(UUID.randomUUID()));
            switchCountryInfoEntity.setState(0);
            switchCountryInfoEntity.setCreateTime(new Date());
            switchCountryInfoEntity.setModifyTime(new Date());
            switchCountryInfoEntities.add(switchCountryInfoEntity);
        }
        if (ObjectUtils.isEmpty(switchCountryInfolists) || switchCountryInfolists.size() == 0) {
            new Result<>(new ArrayList());
        }
        return new Result<>(switchCountryInfolists);
    }
}
